package smartclass.dao;

import java.util.ArrayList;

import smartclass.data.ManagerDataBaseHelper;
import smartclass.data.TableItem;
import smartclass.entity.TestStudentInfo;
import smartclass.present.LogInActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TestStudentDataDao {

	static TestStudentDataDao testStudentDao;
	ManagerDataBaseHelper db;
	String tableName;

	public TestStudentDataDao() {
		db = LogInActivity.db;
		tableName = ManagerDataBaseHelper.TESTSTUDENT_TABLE;
	}

	public static TestStudentDataDao getInstance() {
		if (testStudentDao == null) {
			testStudentDao = new TestStudentDataDao();
		}
		return testStudentDao;
	}

	// Add Item
	public boolean addTestStudent(TestStudentInfo testStudent) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");

		ContentValues value = new ContentValues();
		value.put(TableItem.NAME, testStudent.getName());
		value.put(TableItem.ID_HOC_SINH, testStudent.getIdStudent());
		value.put(TableItem.ID_GIAO_VIEN, testStudent.getIdTeacher());
		value.put(TableItem.PATH, testStudent.getPath());
		data.insert(tableName, null, value);
		data.close();
		return true;
	}

	// Get Item
	public TestStudentInfo getTestStudent(int id) {
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		TestStudentInfo testStudentInfo = new TestStudentInfo();
		Cursor c = null;

		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ TableItem.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			testStudentInfo.setId(Integer.parseInt(c.getString(0)));
			testStudentInfo.setName(c.getString(1));
			testStudentInfo.setIdStudent(Integer.parseInt(c.getString(2)));
			testStudentInfo.setIdTeacher(Integer.parseInt(c.getString(3)));
			testStudentInfo.setPath(c.getString(4));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return testStudentInfo;
	}

	// Get All Item
	public ArrayList<TestStudentInfo> getAllTestStudent() {
		ArrayList<TestStudentInfo> allTestStudent = new ArrayList<TestStudentInfo>();
		Cursor c = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		String sql = "SELECT * FROM " + tableName;
		c = db.getReadableDatabase().rawQuery(sql, null);
		TestStudentInfo testStudentInfo = null;
		if (c.moveToFirst()) {
			do {
				testStudentInfo = new TestStudentInfo();
				testStudentInfo.setId(Integer.parseInt(c.getString(0)));
				testStudentInfo.setName(c.getString(1));
				testStudentInfo.setIdStudent(Integer.parseInt(c.getString(2)));
				testStudentInfo.setIdTeacher(Integer.parseInt(c.getString(3)));
				testStudentInfo.setPath(c.getString(4));
			} while (c.moveToNext());
		}
		return allTestStudent;
	}

	// Edit Item

	public boolean updateTestStudent(TestStudentInfo testStudent) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		ContentValues value = new ContentValues();
		value.put(TableItem.NAME, testStudent.getName());
		value.put(TableItem.ID_HOC_SINH, testStudent.getIdStudent());
		value.put(TableItem.ID_GIAO_VIEN, testStudent.getIdTeacher());
		value.put(TableItem.PATH, testStudent.getPath());
		data.update(tableName, value, TableItem.ID + "=?",
				new String[] { String.valueOf(testStudent.getId()) });
		data.close();
		return true;
	}

	// Delete Item

	public boolean deleteDirection(int id) {
		String sql = "DELETE FROM " + tableName + " WHERE " + TableItem.ID
				+ "=" + id;
		db.getWritableDatabase().execSQL(sql);
		return true;
	}

}
